package com.gun.management.module.raffle.service.lotteryruleref;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.gun.management.module.raffle.controller.admin.lotteryruleref.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryruleref.LotteryRuleRefDO;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.util.object.BeanUtils;

import com.gun.management.module.raffle.dal.mysql.lotteryruleref.LotteryRuleRefMapper;

import static com.gun.management.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.gun.management.module.raffle.enums.ErrorCodeConstants.*;

/**
 * 抽奖-规则信息 Service 实现类
 *
 * @author 测试账号
 */
@Service
@Validated
public class LotteryRuleRefServiceImpl implements LotteryRuleRefService {

    @Resource
    private LotteryRuleRefMapper lotteryRuleRefMapper;

    @Override
    public Long createLotteryRuleRef(LotteryRuleRefSaveReqVO createReqVO) {
        // 插入
        LotteryRuleRefDO lotteryRuleRef = BeanUtils.toBean(createReqVO, LotteryRuleRefDO.class);
        lotteryRuleRefMapper.insert(lotteryRuleRef);
        // 返回
        return lotteryRuleRef.getId();
    }

    @Override
    public void updateLotteryRuleRef(LotteryRuleRefSaveReqVO updateReqVO) {
        // 校验存在
        validateLotteryRuleRefExists(updateReqVO.getId());
        // 更新
        LotteryRuleRefDO updateObj = BeanUtils.toBean(updateReqVO, LotteryRuleRefDO.class);
        lotteryRuleRefMapper.updateById(updateObj);
    }

    @Override
    public void deleteLotteryRuleRef(Long id) {
        // 校验存在
        validateLotteryRuleRefExists(id);
        // 删除
        lotteryRuleRefMapper.deleteById(id);
    }

    private void validateLotteryRuleRefExists(Long id) {
        if (lotteryRuleRefMapper.selectById(id) == null) {
            throw exception(LOTTERY_RULE_REF_NOT_EXISTS);
        }
    }

    @Override
    public LotteryRuleRefDO getLotteryRuleRef(Long id) {
        return lotteryRuleRefMapper.selectById(id);
    }

    @Override
    public PageResult<LotteryRuleRefDO> getLotteryRuleRefPage(LotteryRuleRefPageReqVO pageReqVO) {
        return lotteryRuleRefMapper.selectPage(pageReqVO);
    }

}